package com.leetcode.根据数据结构分类.并查集;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: xiaomi
 * @date: 2021/1/27
 * @description:
 */
public class C_1579_保证图可完全遍历 {

    public static void main(String[] args) {
        int n = 4;
        int[][] edges = {{3, 1, 2}, {3, 2, 3}, {1, 1, 3}, {1, 2, 4}, {1, 1, 2}, {2, 3, 4}};
        C_1579_保证图可完全遍历 action = new C_1579_保证图可完全遍历();
        int res = action.maxNumEdgesToRemove(n, edges);
        System.out.println("res = " + res);

    }

    /**
     * 从结果出发，分析重合边的重要性 > 独立的边
     * 1.找到与类型3 重合的边；处理完公共边，就不需要将 A 和 B 混起来考虑了，问题就比较明朗了。
     * 2.
     *
     * @param n     顶点的个数
     * @param edges 边的数量
     * @return
     */
    public int maxNumEdgesToRemove(int n, int[][] edges) {
        List<Integer> aList = new ArrayList<>();
        List<Integer> bList = new ArrayList<>();

        int len = edges.length;
        InnerUnionFind unionFind = new InnerUnionFind(n+1);

        int res = 0;
        for (int i = 0; i < len; i++) {
            int type = edges[i][0];
            if (type == 3) {
                //公共边
                if (!unionFind.isSame(edges[i][1], edges[i][2])) {
                    unionFind.union(edges[i][1], edges[i][2]);
                } else {
                    res++;
                }
            } else if (type == 1) {
                aList.add(i);
            } else {
                bList.add(i);
            }
        }
        //将 unionFind 复制一份，分别作为 Alice 和 Bob 的，讨论独立边
        InnerUnionFind bUnionFind = unionFind.copy();
        //开始进行A 的独立边处理
        for (int i = aList.size() - 1; i >= 0; i--) {
            if (!unionFind.isSame(edges[aList.get(i)][1], edges[aList.get(i)][2])) {
                unionFind.union(edges[aList.get(i)][1], edges[aList.get(i)][2]);
            } else {
                res++;
            }
        }

        for (int i = bList.size() - 1; i >= 0; i--) {
            if (!bUnionFind.isSame(edges[bList.get(i)][1], edges[bList.get(i)][2])) {
                bUnionFind.union(edges[bList.get(i)][1], edges[bList.get(i)][2]);
            } else {
                res++;
            }
        }

        if (unionFind.count > 2 || bUnionFind.count > 2) {
            return -1;
        } else {
            return res;
        }
    }

    class InnerUnionFind {
        int[] parents;
        int[] ranks;
        //连通分量的个数
        int count;

        public InnerUnionFind(int capacity) {
            parents = new int[capacity];
            ranks = new int[capacity];
            for (int i = 0; i < capacity; i++) {
                parents[i] = i;
                ranks[i] = 1;
            }
            count = capacity;
        }

        int find(int index) {
            if (index != parents[index]) {
                parents[index] = find(parents[index]);
            }
            return parents[index];
        }

        void union(int index1, int index2) {
            int p1 = find(index1);
            int p2 = find(index2);
            if (p2 == p1) {
                return;
            }
            count--;
            if (ranks[p1] == ranks[p2]) {
                parents[p1] = parents[p2];
                ranks[p2]++;
            } else if (ranks[p1] < ranks[p2]) {
                parents[p1] = parents[p2];
            } else {
                parents[p2] = parents[p1];
            }
        }

        boolean isSame(int index1, int index2) {
            return find(index1) == find(index2);
        }

        InnerUnionFind copy() {
            int len = this.parents.length;
            InnerUnionFind res = new InnerUnionFind(len);
            res.count = this.count;
            int[] ints = new int[len];
            System.arraycopy(this.parents, 0, ints, 0, len);
            res.parents = ints;

            int[] tempRanks = new int[len];
            System.arraycopy(this.ranks, 0, tempRanks, 0, len);
            res.ranks = tempRanks;
            return res;
        }
    }

}
